var prefix = ctx + "admin/user";

$(function() {
	var panehHidden = false;
	if ($(this).width() < 769) {
		panehHidden = true;
	}
	$('body').layout({
		initClosed : panehHidden,
		west__size : 185
	});
	queryUserList();
	queryDeptTree();
});

function queryUserList() {
	var options = {
		url : prefix + "/page",
		updateUrl : prefix + "/goEditUser",
		removeUrl : prefix + "/{id}",
		exportUrl : prefix + "/export",
		importUrl : prefix + "/importData",
		importTemplateUrl : prefix + "/importTemplate",
		sortName : "createTime",
		sortOrder : "desc",
		modalName : "用户",
		columns : [
				{
		            checkbox: true
		        },
		        {
					field : 'userId',
					title : '用户Id',
					visible: false
				},
				{
					field : 'loginname',
					title : '登录名称'
				},
				{
					field : 'username',
					title : '用户名称'
				},
				{
					field : 'deptName',
					title : '所属部门'
				},
				{
					field : 'phone',
					title : '手机'
				},
				{
					visible : editFlag == 'hidden' ? false : true,
					title : '用户状态',
					align : 'center',
					formatter : function(value, row, index) {
						return statusTools(row);
					}
				},
				{
					field : 'createTime',
					title : '创建时间'
				},
				{
					title : '操作',
					align : 'center',
					formatter: function(value, row, index) {
		                var actions = [];
		                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editTab(\'' + row.userId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
		                actions.push('<a class="btn btn-danger btn-xs ' + deleteFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.userId + '\')"><i class="fa fa-remove"></i>删除</a> ');
		                var more = [];
		                more.push("<a class='btn btn-default btn-xs' href='javascript:void(0)' onclick='resetPwd(" + row.userId + ")'><i class='fa fa-key'></i>重置密码</a> ");
		                more.push("<a class='btn btn-default btn-xs " + editFlag + "' href='javascript:void(0)' onclick='authRole(" + row.userId + ")'><i class='fa fa-check-square-o'></i>分配角色</a>");
		                actions.push('<a tabindex="0" class="btn btn-info btn-xs" role="button" data-container="body" data-placement="left" data-toggle="popover" data-html="true" data-trigger="hover" data-content="' + more.join('') + '"><i class="fa fa-chevron-circle-right"></i>更多操作</a>');
		                return actions.join('');
		            }
				} ]
	};
	$.table.init(options);
}

function queryDeptTree() {
	var url = ctx + "admin/dept/tree";
	var options = {
		url : url,
		expandLevel : 1,
		onClick : zOnClick
	};
	$.tree.init(options);

	function zOnClick(event, treeId, treeNode) {
		$("#deptId").val(treeNode.id);
		$.table.search();
	}
}

function reset(){
	$("#deptId").val('');
	$.form.reset();
}

$('#btnExpand').click(function() {
	$._tree.expandAll(true);
	$(this).hide();
	$('#btnCollapse').show();
});

$('#btnCollapse').click(function() {
	$._tree.expandAll(false);
	$(this).hide();
	$('#btnExpand').show();
});

$('#btnRefresh').click(function() {
	queryDeptTree();
	$("#deptId").val('');
	$.table.search();
});

/* 用户管理-重置密码 */
function resetPwd(userId) {
	var url = prefix + '/resetPwd/' + userId;
	$.modal.open("重置密码", url, '800', '300');
}

/* 用户管理-分配角色 */
function authRole(userId) {
	var url = prefix + '/authRole/' + userId;
	$.modal.openTab("用户分配角色", url);
}

/* 用户状态显示 */
function statusTools(row) {
	if (row.lockFlag == 9) {
		return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\''
				+ row.userId + '\')"></i> ';
	} else {
		return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\''
				+ row.userId + '\')"></i> ';
	}
}

/* 用户管理-停用 */
function disable(userId) {
	$.modal.confirm("确认要停用用户吗？", function() {
		$.operate.post(prefix + "/changeStatus", {
			"userId" : userId,
			"lockFlag" : 9
		});
	})
}

/* 用户管理启用 */
function enable(userId) {
	$.modal.confirm("确认要启用用户吗？", function() {
		$.operate.post(prefix + "/changeStatus", {
			"userId" : userId,
			"lockFlag" : 0
		});
	})
}